package com.qf.service.impl;


import com.qf.dao.AccountDao;
import com.qf.dao.LogDao;
import com.qf.dao.ProductDao;
import com.qf.dao.impl.AccountDaoImpl;
import com.qf.dao.impl.LogDaoImpl;
import com.qf.dao.impl.ProductDaoImpl;
import com.qf.model.Card;
import com.qf.model.Log;
import com.qf.model.User;
import com.qf.model.pojo.UserCard;
import com.qf.service.AccountService;
import com.qf.utils.DruidUtil;

import java.sql.Connection;
import java.sql.Timestamp;
import java.util.List;
@SuppressWarnings("all")
public class AccountServiceImpl implements AccountService {
    Connection connection = DruidUtil.getConnection();
    AccountDao accountDao=new AccountDaoImpl();
    ProductDao productDao=new ProductDaoImpl();
    LogDao logDao=new LogDaoImpl();

    @Override
    public UserCard findCardById(String c_id) {
        return accountDao.findCardById(c_id);
    }


    @Override
    public List<UserCard> findUid(String u_id) {
        return accountDao.findUid(u_id);
    }


    //根据银行卡号 查询储蓄银行卡信息

    @Override
    public Card findCardByCid(String cid) {
        return accountDao.findCardByCid(cid);
    }


    //修改转入、转出信息
    @Override
    public int updateMoney(String c_id, int a, Double transferMoney) {
        return accountDao.updateMoney(c_id, a, transferMoney);
    }




    //修改转入、转出信息
    @Override
    public int transfer(String cid1, String cid2, Double transferMoney) {
        //开启事务
        DruidUtil.begin(connection);

        //调用dao层transfer方法获取到返回值来判断事务的提交和回滚
        //int i = accountDao.transfer(connection, cid1, cid2, transferMoney);
        int i1 = productDao.lessenCardMoney(connection, cid1, transferMoney);  //付款
        int i2 = productDao.addCardMoney(connection, cid2, transferMoney);     //收款

       //通过收付款银行卡id，调用方法查到usercard联表，获取用户所有信息
        UserCard user1 = accountDao.findCardById(cid1);
        UserCard user2 = accountDao.findCardById(cid2);
        //创建log对象，并赋值
       Log log1 = new Log(
                user1.getU_idcard(),
                user1.getU_phone(),
                cid1,
                transferMoney,
                1, //扣钱
                //new Date(new java.util.Date().getTime()),
                new Timestamp(new java.util.Date().getTime()),
                3,
               "收款账户："+cid2
        );
        Log log2 = new Log(
                user2.getU_idcard(),
                user2.getU_phone(),
                cid2,
                transferMoney,
                2, //价钱
                //new Date(new java.util.Date().getTime()),
                new Timestamp(new java.util.Date().getTime()),
                3,
                "付款账户："+cid1
        );
        //调用添加日志方法，添加转入、转出的日志文件
        int j1 = logDao.addLog(connection, log1);
        int j2 = logDao.addLog(connection, log2);

        //判断转账语句是否执行成功，日志是否添加成功，如果成功了就提交，否则回滚
        if (i1==1&&i2==1&&j1==1&&j2==1){
            DruidUtil.commit(connection);
            return 1;
        }else {
            DruidUtil.rollback(connection);
            return 0;
        }

    }

    //根据用户Id查找
    @Override
    public User findUserId(String u_id) {
        return accountDao.findUserId(u_id);
    }


    //提现
    @Override
    public int getMoney(String cid, User user, Double outMoney) {
        //开启事务
        DruidUtil.begin(connection);

        //调用dao层getMoney方法获取到返回值来判断事务的提交和回滚
      /*  int i = accountDao.getMoney(connection, cid, user, outMoney);*/
        int i1 = productDao.lessenUserMoney(connection, user.getU_id(), outMoney); //app扣款
        int i2 = productDao.addCardMoney(connection, cid, outMoney);               //银行卡收款
        //通过收款银行卡id，调用方法查到usercard联表，获取用户所有信息
        UserCard userCard = accountDao.findCardById(cid);
        //创建log对象，并赋值
        Log log1=new Log(
                user.getU_idcard(),
                user.getU_phone(),
                user.getU_id().toString(),
                outMoney,
                1, //扣钱
                //new Date(new java.util.Date().getTime()),
                new Timestamp(new java.util.Date().getTime()),
                2,//提现
                "收款账户："+cid
        );
        Log log2 = new Log(
                userCard.getU_idcard(),
                userCard.getU_phone(),
                cid,
                outMoney,
                2, //加钱
                //new Date(new java.util.Date().getTime()),
                new Timestamp(new java.util.Date().getTime()),
                2,//提现
                "用户:"+user.getU_name()+"付款"
        );
        //添加日志
        int j1 = logDao.addLog(connection, log1);
        int j2 = logDao.addLog(connection, log2);

        //判断提现语句是否执行，日志是否执行了，如果执行了就提交，否则回滚
        if (i1==1&&i2==1&&j1==1&&j2==1){
            DruidUtil.commit(connection);
            return 1;
        }else {
            DruidUtil.rollback(connection);
        }
        return 0;
    }

    //充值
    @Override
    public int TopUp( String cid, User user, Double f) {
        Connection connection = DruidUtil.getConnection();
        //开启事务
        DruidUtil.begin(connection);

        //调用dao层getMoney方法获取到返回值来判断事务的提交和回滚
      /*  int i = accountDao.TopUp(connection, cid, user, f);*/
        int i1 = productDao.lessenCardMoney(connection, cid, f);          //银行卡扣款
        int i2 = productDao.addUserMoney(connection, user.getU_id(), f);  //app收款

        //通过付款银行卡id，调用方法查到usercard联表，获取用户所有信息
        UserCard userCard = accountDao.findCardById(cid);
        //创建log对象，并赋值
        Log log1=new Log(
                userCard.getU_idcard(),
                userCard.getU_phone(),
                userCard.getU_id().toString(),
                f,
                1, //扣钱
                //new Date(new java.util.Date().getTime()),
                new Timestamp(new java.util.Date().getTime()),
                1,//充值
                "用户："+user.getU_name()+"收款"
        );
        Log log2 = new Log(
                user.getU_idcard(),
                user.getU_phone(),
                cid,
                f,
                2, //加钱
                //new Date(new java.util.Date().getTime()),
                new Timestamp(new java.util.Date().getTime()),
                1,//充值
                "付款账户："+cid
        );
        //添加日志
        int j1 = logDao.addLog(connection, log1);
        int j2 = logDao.addLog(connection, log2);
        //判断充值语句是否执行，日志是否执行了，如果执行了就提交，否则回滚
        if (i1==1&&i2==1&&j1==1&&j2==1){
            DruidUtil.commit(connection);
            return 1;
        }else {
            DruidUtil.rollback(connection);
        }
        return 0;
    }




}
